package com.fjwt.gz.core.model;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.regex.Pattern;

/**
 * application.yml 移植到DB中的配置变量
 **/
@Data
@Slf4j
public class SmsConfig implements Serializable {

    /** 登录验证码 **/
    public static final String TYPE_AUTH = "auth";
    /** 注册验证码 **/
    public static final String TYPE_REGISTER = "register";
    /** 找回密码验证码 **/
    public static final String TYPE_RETRIEVE = "retrieve";
    /** 账号开通 **/
    public static final String TYPE_ACCOUNT_OPEN = "accountOpen";

    public static final String endpointName = "cn-hangzhou";

    public static final String regionId = "cn-hangzhou";

    /** 产品名称:云通信短信API产品,开发者无需替换 **/
    public static final String product = "Dysmsapi";

    /** 产品域名,开发者无需替换 **/
    public static final String domain = "dysmsapi.aliyuncs.com";

    public static final byte BIZ_TYPE_REGISTER = 10;


    public static final SmsConfig parse(String val){

        if(StringUtils.isEmpty(val)){
            return null;
        }

        try {
            return JSONObject.parseObject(val, SmsConfig.class);
        } catch (Exception e) {
            log.error("SmsConfig数据转换异常", e);
            return null;
        }
    }


    /** id **/
    private String accessKeyId;

    /** key **/
    private String accessKeySecret;

    /** 签名串 **/
    private String signName;

    /** 忘记密码模板ID **/
    private String forgetPwdTemplateId;

    /** 商户注册模板ID **/
    private String registerMchTemplateId;

    /** 商户登录模板ID **/
    private String loginMchTemplateId;

    /** 账号开通提醒模板ID **/
    private String accountOpenTemplateId;


    /**
     * 验证手机号码，11位数字，1开头 *
     * @param mobileNumber
     * @return
     */
    public static boolean checkMobileNumber(String mobileNumber) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^1\\d{10}$");
            flag = regex.matcher(mobileNumber).matches();
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;

        }
        return flag;
    }
}
